import Vue from 'vue'
import api from 'utils/api.js'
let _this = new Vue;
let loginWx = function(isTimeOut, callback) {
	// let _this = new Vue;
	let activeObj = uni.getStorageSync('activeObj');
	console.log(activeObj, '进来了过期请求');
	wx.login({
		success(res) {
			if (res.code) {
				//发起网络请求
				console.log(res, '过期后获得了code');
				_this.http({
					url:`${api.ServerUrlW}api/v1/account/oAuthMiniProgram/${res.code}`,
					method: 'GET',
					success: res => {
						console.log(res, '过期请求返回');
						if (res.code == 0) {
							if (!res.result.oAuth.token) {
								let url = '../index/index'
								login();
								// uni.reLaunch({
								// 	url: url
								// });
							} else { //已经授权过
								console.log('jinqu1l1//第一次进来');
								uni.setStorageSync('token', res.result.oAuth.token);
								uni.setStorageSync('userid', res.result.userInfo.userId);
								// _this.$store.commit('changeToken', res.result.oAuth.token);
								// if (isTimeOut) {
								login();
								var page = getCurrentPages().pop(); //获取当前页面实例
							}

						}
					},
				})
			} else {
				console.log('登录失败！' + res.errMsg)
			}
		}
	})
}
// 获取登录token
let login = function() {
	if(uni.getStorageSync('phone')) {
		_this.http({
			url: `form_v1/account/login`,
			method: 'POST',
			data: {
				mobile: uni.getStorageSync('phone'),
				password: 'cXcxMmFz',
				login_account_type: 1,
				username: ''
			},
			success: res => {
				if(res.errcode == 0) {
					_this.login1(res.result.token);
				}
			},
		})
	}else {
		uni.reLaunch({
			url: '../index/index'
		});
	}
}
// 登录
let login1 = function(token) {
	_this.http({
		url: `authenticate/login`,
		method: 'POST',
		data: {
			loginToken: token,
			loginType: 'password'
		},
		success: res => {
			if(res.errcode == 0) {
				uni.setStorageSync('token', res.result.token_data.token);
			}
		},
	})
}
export default loginWx
